package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.HttpStatus;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.dto.OverCountTimeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 预约信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-15
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        return save(reservation) ? 1 : 0;
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return updateById(reservation) ? 1 : 0;
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询取消预约信息列表
     *
     * @return
     */
    @Override
    public Integer selectReservationOver() {
        Long userId = UserThreadLocal.getUserId();
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        return reservationMapper.selectReservationOver(startTime,endTime,userId);
    }

    /**
     * 查询时间段剩余预约次数
     *
     * @param time
     * @return
     */
    @Override
    public List<OverCountTimeDto> selectReservationOverCount(Long time) {
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        return reservationMapper.selectReservationOverCount(startTime,endTime);
    }

    /**
     * 预约信息
     *
     * @param reservation
     */
    @Override
    public void insertReservationBy(Reservation reservation) {
        Long userId = UserThreadLocal.getUserId();
        reservation.setCreateBy(userId.toString());
        reservation.setUpdateBy(userId.toString());
        reservation.setStatus(0);
        reservation.setCreateTime(DateUtils.getNowDate());
        reservation.setUpdateTime(DateUtils.getNowDate());
        reservationMapper.insertReservation(reservation);
    }

    /**
     * 分页查询预约信息
     *
     * @param pageSize
     * @param pageNum
     * @param status
     * @return
     */
    @Override
    public TableDataInfo selectReservationPage(Integer pageSize, Integer pageNum, Integer status) {
        Long userId = UserThreadLocal.getUserId();
        Page<Reservation> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Reservation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(status!=null,Reservation::getStatus, status);
        page(page, queryWrapper);
        TableDataInfo<Reservation> tableDataInfo = new TableDataInfo<>();
        //设置数据
        tableDataInfo.setRows(page.getRecords());
        //总记录数
        tableDataInfo.setTotal(page.getTotal());
        //设置状态和消息
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        //返回封装的数据
        return tableDataInfo;
    }

    /**
     * 更新预约信息
     *
     * @param id
     */
    @Override
    public void updateReservationByID(Long id) {
        reservationMapper.updateReservationByID(id);
    }
}
